package dingService

import (
	"encoding/json"
	"github.com/astaxie/beego/logs"
	"net/url"
	"strconv"
	"wonlicsys/constPack"
	"wonlicsys/pkg/dingding"
	"wonlicsys/utils/tool"
)

type DingCallBackService struct {
	Response map[string]interface{}
}

func (dingService *DingCallBackService) CallBackService(query url.Values, Encrypt string) error {
	//返回加密的json 包含success的加密字符串
	signature := query.Get("signature")
	timestamp := query.Get("timestamp")
	nonce := query.Get("nonce")
	logs.Debug(signature, timestamp, nonce, Encrypt, "调试接受到的数据")
	dingTalkCrypto := dingding.NewDingTalkCrypto(constPack.DingToken, constPack.DingAesKey, constPack.DingAppKey)
	decryptMsg, err := dingTalkCrypto.GetDecryptMsg(signature, timestamp, nonce, Encrypt)
	if err != nil {
		return err
	}
	//todo 解密的信息≈逻辑处理
	resJson, _ := json.Marshal(decryptMsg)
	logs.Debug(string(resJson), "调试")

	//返回加密的json 包含success的加密字符串
	respTimestamp := strconv.FormatInt(tool.NewGoTime("").Timestamps(), 10)
	resNonce := tool.GetNewUuid()
	resencrytyMsg, resMsgSignature, err := dingTalkCrypto.GetEncryptMsg("success", respTimestamp, resNonce)
	if err != nil {
		return err
	}
	resMap := map[string]interface{}{
		"msg_signature": resMsgSignature,
		"timeStamp":     respTimestamp,
		"nonce":         resNonce,
		"encrypt":       resencrytyMsg,
	}
	dingService.Response = resMap
	return nil
}
